package 题目集.树.反转树;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

//https://pintia.cn/problem-sets/1739473818167930880/exam/problems/1739473861503483914?type=7&page=0
public class 玩转二叉树 {
    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        int[] ord = new int[n];
        int[] pre = new int[n];
        for (int i = 0; i < n; i++) {
            ord[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            pre[i] = sc.nextInt();
        }
        Tree tree = build(ord, pre, 0, 0, n);
        reversal(tree);
        levelPrint(tree);
    }

    public static Tree build(int[] order, int[] preorder, int ordI, int preI, int len) {
        if (len < 1) {
            return null;
        }
        Tree curr = new Tree(preorder[preI]);
        int index = -1;
        for (int i = ordI; i < ordI + len; i++) {
            if (order[i] == preorder[preI]) {  //找到中序根节点的位置
                index = i;
                break;
            }
        }
        curr.left = build(order, preorder, ordI, preI + 1, index - ordI);
        //右子树
        curr.right = build(order, preorder, index + 1, preI + (index - ordI) + 1, len - (index - ordI) - 1);
        return curr;
    }

    public static void reversal(Tree tree) {
        if (tree == null) {
            return;
        }
        reversal(tree.left);
        reversal(tree.right);
        Tree temp = tree.right;
        tree.right = tree.left;
        tree.left = temp;
    }

    public static void levelPrint(Tree tree) {
        Queue<Tree> queue = new LinkedList<>();
        queue.offer(tree);
        StringBuilder builder = new StringBuilder();
        while (!queue.isEmpty()) {
            Tree poll = queue.poll();
            builder.append(poll.val).append(" ");
            if (poll.left != null) {
                queue.offer(poll.left);
            }
            if (poll.right != null) {
                queue.offer(poll.right);
            }
        }
        System.out.println(builder.substring(0, builder.length() - 1));
    }
//
//    static {
//        int[] ord = {1, 2, 3, 4, 5, 6, 7};
//        int[] pre = {4, 1, 3, 2, 6, 5, 7};
//        Tree build = build(ord, pre, 0, 0, ord.length);
//        reversal(build);
//        levelPrint(build);
//    }

    static class Tree {
        int val;
        Tree left;
        Tree right;

        public Tree(int val) {
            this.val = val;
        }
    }

    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}

